package rigcontrol.relationData;

import BaseEnvi.HiberTestCase;
import edu.scau.comeva.rigcontrol.entity.*;
import org.junit.Test;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * Created with IntelliJ IDEA.
 * User: Dan
 * Date: 13-8-20
 * Time: 下午8:42
 * To change this template use File | Settings | File Templates.
 */
public class setRelationData extends HiberTestCase{

    @Test
    public void setRoleModule(){
        List<m_role> roles = session.createQuery("from m_role role order by role.role_id asc").list();

        String[] kind_1 = {"1","2","5","7","9","10","11"};
        String[] kind_2 = {"1","3","5","7","12","13"};
        String[] kind_3 = {"1","4","5","6","7","8","14","15","16","17","18","19","20","21","22","23"};

        Set<m_module> modules_1 = new HashSet<m_module>();
        m_role role_1 = roles.get(0);
        assertEquals(role_1.getRole_id(),1);
        for(String str : kind_1){
            long id = Long.parseLong(str);
            m_module module = (m_module) session.get(m_module.class,id);
            modules_1.add(module);
        }
        role_1.setRole_modules(modules_1);

        Set<m_module> modules_2 = new HashSet<m_module>();
        m_role role_2 = roles.get(1);
        assertEquals(role_2.getRole_kind(),2);
        for(String str : kind_2){
            long id = Long.parseLong(str);
            m_module module = (m_module) session.get(m_module.class,id);
            modules_2.add(module);
        }
        role_2.setRole_modules(modules_2);

        Set<m_module> modules_3 = new HashSet<m_module>();
        m_role role_3 = roles.get(2);
        assertEquals(role_3.getRole_id(),3);
        for(String str : kind_3){
            long id = Long.parseLong(str);
            m_module module = (m_module) session.get(m_module.class,id);
            modules_3.add(module);
        }
        role_3.setRole_modules(modules_3);
    }

    @Test
    public void setClassStudent(){
        List<m_class> classes = session.createQuery("from m_class cls order by cls.class_id asc ").setMaxResults(2).list();
        List<m_student> students = session.createQuery("from m_student student order by student.student_id asc ").list();

        assertEquals(classes.size(),2);
        assertEquals(students.size(),5);

        int i = 0;

        for(m_student student : students){
            i++;
            System.out.println(i);
            if(i<=3){
                student.setStudent_class(classes.get(0));
                session.flush();
            }else{
                student.setStudent_class(classes.get(1));
                session.flush();
            }
        }
    }

    @Test
    public void testClassDeterTeam(){
        List<m_class> classes = session.createQuery("from m_class cls order by cls.class_id asc ").setMaxResults(2).list();
        List<m_deterTeam> deterTeams = session.createQuery("from m_deterTeam deterTeam order by deterTeam.deterTeam_id asc").setMaxResults(2).list();

        assertEquals(classes.size(),2);
        assertEquals(deterTeams.size(),2);

        int i=0;

        for(m_deterTeam deterTeam : deterTeams){
            deterTeam.setDeterTeam_class(classes.get(i));
            session.flush();
            i++;
        }

    }

    @Test
    public void testStudentDeatal(){
        List<m_student> students = session.createQuery("from m_student student order by student.student_id asc ").list();
        List<m_studentDetal> studentDetals = session.createQuery("from m_studentDetal studentDetal order by studentDetal.student_detal_id asc ").list();

        assertEquals(studentDetals.size(),students.size());

        for(int i=0;i<students.size();i++){
            studentDetals.get(i).setStudentdetal_student(students.get(i));
            session.flush();
        }
    }
}
